package me.idashu.attg.util;
import java.util.Date;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SimpleMailSender {
	public void sendTestMail(String toAdd, String userName) throws AddressException, MessagingException
    {
        Properties mailProps = new Properties();
        mailProps.setProperty("mail.smtp.host", "smtp.163.com");
        mailProps.setProperty("mail.smtp.starttls.enable", "true");
        mailProps.setProperty("mail.smtp.auth", "true");
        mailProps.setProperty("mail.smtp.quitwait", "false");
 
        //���Ҫ�Է�������ssl֤����������μ�飬������������
 
        //mailProps.setProperty("mail.smtp.ssl.trust","*");

        Session mailSession = Session.getDefaultInstance(mailProps,
                new Authenticator()
                {
                    protected PasswordAuthentication getPasswordAuthentication()
                    {
                        return new PasswordAuthentication("tianpeng615@163.com", "151730");
                    }
                });

        Message mailMessage = new MimeMessage(mailSession);
        mailMessage.setFrom(new InternetAddress("tianpeng615@163.com"));
        mailMessage.setRecipients(Message.RecipientType.TO,
                InternetAddress.parseHeader(toAdd, false));

        mailMessage.setSubject("用户注册时输入以下验证码激活！");
        mailMessage.setText("本次注册验证码为："+userName);
        mailMessage.setSentDate(new Date());

        Transport.send(mailMessage);
    }
}

